package com.example.rwq_test.aspect.annoAspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

/**
 * @Description service日志切面增强
 *
 * @Author renweiqiang
 * @Date 2024/7/18 下午3:56
 */
@Slf4j
@Aspect
@Component
public class ServiceAspect {
    @Pointcut("@annotation(com.example.rwq_test.aspect.annoAspect.LogAnnotation)")
    public void serviceLogAround(){

    }

    @Around("serviceLogAround()")
    public Object serviceLogAround(ProceedingJoinPoint proceedingJoinPoint) {
        MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
        String method = signature.getMethod().getName();
        String className = proceedingJoinPoint.getTarget().getClass().getName();
        long start = System.currentTimeMillis();
        log.info(className + "." + method + "start.");
        Object result = null;
        try {
            result = proceedingJoinPoint.proceed();
        } catch (Throwable e) {
            log.error(e.getMessage());
        }
        log.info(className + "." + method + "finish. use time: {}", System.currentTimeMillis() - start);
        return result;
    }
}
